package com.maila.company.iterget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by 昂首天下 on 2017/8/14.
 */

public class UpdataManager {

    private static final String VERSION_PATH = "http://10.10.10.90:7800/app/goods/updateApk";
    private static final int DOWNLOAD_ING = 1;
    private static final int DOWNLOAD_OVER = 2;
    private int serverVersion;
    private String version_name;
    private String version_desc;
    private String version_path;
    private Context mContext;
    private String mSavePath;
    private ProgressBar mProgress;
    private int progress;
    private Dialog downDialog;
    private Boolean isCancle = false;
    public UpdataManager(Context context){
        mContext = context;
    }

    private Handler versionHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            JSONObject jsonObject = (JSONObject) msg.obj;
            try {
                serverVersion = jsonObject.getInt("version_code");
                version_name = jsonObject.getString("version_name");
                version_desc = jsonObject.getString("version_desc");
                version_path = jsonObject.getString("version_path");

                if (isUpdate()){
                    Toast.makeText(mContext, "需要更新", Toast.LENGTH_SHORT).show();
                    // 显示提示更新对话框
                    showNoticeDialog();
                } else{
                    Toast.makeText(mContext, "已是最新版本", Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    private Handler progressHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case DOWNLOAD_ING:
                    mProgress.setProgress(progress);
                    break;
                case DOWNLOAD_OVER:
                    downDialog.dismiss();
                    installAPK();
                    break;
            }
        }
    };

    /**
     *检测软件是否需要更新
     * */
    public void checkUpdate() {

        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        JsonObjectRequest request = new JsonObjectRequest(VERSION_PATH, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Message msg = Message.obtain();
                msg.obj = jsonObject;
                versionHandler.sendMessage(msg);
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                System.out.println(arg0.toString());
                Log.i("xian",arg0.toString());
            }
        });
        //添加到请求队列中
        requestQueue.add(request);

    }

    /**
     * 是否需要更新
     * @return
     */
    protected boolean isUpdate() {

        int localVersion = 1;
        try {
            localVersion = mContext.getPackageManager().getPackageInfo("com.maila.project.company.mailaapplication",0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if(serverVersion > localVersion){
            return  true;
        }
        return  false;
    }


    /*
    * 有更新时显示提示对话框
    */
    protected void showNoticeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("发现新版本");
        builder.setMessage("需要升级吗? \n" + version_desc);
        builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消对话框
                dialog.dismiss();
                //下载文件
                showDownloadDialog();
            }
        });

        builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消对话框
                dialog.dismiss();
            }
        });
        builder.create().show();
    }


    /*
     * 显示正在下载对话框
     */
    protected void showDownloadDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.down_progress,null);
        mProgress = (ProgressBar) view.findViewById(R.id.id_Progress);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("正在下载");
        builder.setView(view);
        builder.setNegativeButton("取消下载", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                isCancle = true;
            }
        });
        downDialog = builder.create();
        downDialog.show();

        downloadAPK();
    }

    /*
    * 开启新线程下载文件
    */
    private void downloadAPK() {
        System.out.println(version_path);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        String sdPath = Environment.getExternalStorageDirectory() + "/";
                        mSavePath = sdPath +"deanDownload";
                        File file = new File(mSavePath);
                        if(!file.exists()){
                            file.mkdir();
                        }

                        URL url = new URL(version_path);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.connect();
                        InputStream is = connection.getInputStream();

                        int len = connection.getContentLength();
                        File apkFile = new File(mSavePath, version_name);
                        FileOutputStream fos = new FileOutputStream(apkFile);
                        int count = 0;
                        byte[] buffer = new byte[1024];
                        while(!isCancle){
                            int numread = is.read(buffer);
                            count += numread;
                            // 计算进度条的当前位置
                            progress = (int) (((float)count/len) * 100);
                            // 更新进度条
                            progressHandler.sendEmptyMessage(DOWNLOAD_ING);

                            // 下载完成
                            if (numread < 0){
                                progressHandler.sendEmptyMessage(DOWNLOAD_OVER);
                                break;
                            }
                            fos.write(buffer, 0, numread);
                        }
                    }else{
                        System.out.println("00000000000000000000000000000");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }


    /*
     * 下载到本地后执行安装
     */
    protected void installAPK() {
        File apkFile = new File(mSavePath, version_name);
        if (!apkFile.exists())
            return;

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("file://" + apkFile.toString());
        intent.setDataAndType(uri, "iterget/vnd.android.package-archive");
        mContext.startActivity(intent);
    }
}
